Covid19 Visualization of India Using Python (Till 22-Sept-2020)

Importing the modules

In [1]:
import numpy
import pandas as pd
import datetime
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objs as go
%matplotlib inline

Loading the Dataset

In [2]:
df = pd.read_csv('covid_19_india.csv')
dt = df['Date']                             # Restore Date in another variable to preserve its d_type

Data Preprocessing

In [3]:
df['Date'] = pd.to_datetime(df['Date'], format='%d-%m-%Y')
df = df.sort_values(by='Date',ascending=True)
In [4]:
df.columns
Out[4]:
Index(['Sno', 'Date', 'Time', 'State/UnionTerritory',
       'ConfirmedIndianNational', 'ConfirmedForeignNational', 'Cured',
       'Deaths', 'Confirmed'],
      dtype='object')
In [5]:
df = df.drop(['Sno', 'Time', 'ConfirmedForeignNational', 'ConfirmedIndianNational'], axis = 1)
df.columns
Out[5]:
Index(['Date', 'State/UnionTerritory', 'Cured', 'Deaths', 'Confirmed'], dtype='object')

Covid-19 Data Visualization

Using Simple Plot to represent Rate of Confirmed Covid19 Cases

In [6]:
fig = plt.figure(figsize =(12, 4.8))
plt.plot(df.Date,df.Confirmed)
plt.title("COVID-19 INDIA")
plt.xticks(rotation=60)
plt.xlabel("Date",labelpad=10)
plt.ylabel("Number of Cases",labelpad=10)
plt.show()

Using Simple Plot to represent Rate of Deaths due to Covid19

In [7]:
fig = plt.figure(figsize =(12, 4.8))
plt.plot(df.Date,df.Deaths,color='red')
plt.title("COVID-19 INDIA")
plt.xticks(rotation=60)
plt.xlabel("Date",labelpad=10)
plt.ylabel("Number of Deaths",labelpad=10)
plt.show()

Using Line Plot to represent Rate of Confirmed, Recovered (Cured), Deaths Cases

In [8]:
sw_date = df.melt(id_vars='Date', value_vars=['Confirmed', 'Cured', 'Deaths'], var_name='Ratio', value_name='Value')
fig = px.line(sw_date, x="Date", y="Value", color='Ratio',)
fig.show()

Using Bar Plot "State/UnionTerritory" vs "No. of Confirmed Cases"

In [9]:
fig = plt.figure(figsize =(12, 4.8))
ax = fig.add_axes([0,0,1,1])
ax.bar(df['State/UnionTerritory'],df['Confirmed'], color = list('rgbkymc'), width = 0.7,align='center')
plt.title('COVID-19 INDIA')
plt.xticks(rotation=90)
plt.xlabel('State Name')
plt.ylabel('Number of Cases')
plt.show()

Using HeatMap

In [10]:
fig = plt.figure(figsize =(12, 4.8))
pvt = pd.pivot_table(df,index='State/UnionTerritory', values='Confirmed', columns='Date')

sns.heatmap(pvt,xticklabels=False, yticklabels=True,cmap="YlGnBu")
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x26076bd36c8>

Animated

*** TreeMap Plot for "State Wise Display Based on Total Number of Confirmed Cases"

In [11]:
fig = px.treemap(df, path=['State/UnionTerritory'], values='Confirmed', height=600, width=1000, title = 'State Wise Display Based on Total Number of Confirmed Cases')
#fig.update_layout(title_x = 0.5,geo=dict(showframe = True,showcoastlines = True))
fig.show()

Visualization Using Histogram

In [12]:
fig = px.histogram(df, x="Confirmed", y="Cured", color="State/UnionTerritory", marginal="rug", hover_data=df.columns)
fig.show()

Visualization Using Line Plot of "Rate of Confirmed Cases and Deaths"

In [13]:
px.line(df,x='Date',y=['Confirmed','Deaths'], animation_frame = df['State/UnionTerritory'])

Visualization Using Bar Plot of "State/UnionTerritory" vs "Confirmed Cases" with date

In [14]:
px.bar(df,x='State/UnionTerritory',y='Confirmed',color='Date',title='Covid - 19 - India',log_y=True)

Visualization Using Bar Plot of "State/UnionTerritory" vs "Deaths" with date

In [15]:
px.bar(df,x='State/UnionTerritory',y='Deaths',color='Date',title='Covid - 19 - India',log_y=True)

Visualization Using Pie-Plot of "Confirmed COVID19 Cases in Percentage"

In [16]:
cases = df[df['Date']==df['Date'].max()]['Confirmed']
name = df[df['Date']==df['Date'].max()]['State/UnionTerritory']
fig = px.pie(df, values=cases, names=name, title ="Confirmed COVID19 Cases in Percentage")
fig.show()

Visualization Using Pie-Plot of "Deaths due to COVID19 in Percentage"

In [17]:
cases = df[df['Date']==df['Date'].max()]['Deaths']
name = df[df['Date']==df['Date'].max()]['State/UnionTerritory']
fig = px.pie(df, values=cases, names=name, title ="Deaths by COVID19 in Percentage")
fig.show()

Visualization Using Scatter Plot of "States Based on Total No. of Confirmed Cases Recorded against Cured Cases"

In [18]:
cases = df[df['Date']==df['Date'].max()]['Confirmed']
name = df[df['Date']==df['Date'].max()]['State/UnionTerritory']
cured = df[df['Date']==df['Date'].max()]['Cured']
fig = px.scatter(df, x=cases, y=cured, size=cured, color=name, hover_name=name, log_x=True, size_max=60, title ="States Based on Total Number of Positive Cases Recorded against Non-Active Cases")
fig.show()

Visualization Using Scatter Plot of "States Based on Total No. of Confirmed Cases Recorded against Deaths"

In [19]:
cases = df[df['Date']==df['Date'].max()]['Confirmed']
name = df[df['Date']==df['Date'].max()]['State/UnionTerritory']
deaths = df[df['Date']==df['Date'].max()]['Deaths']
fig = px.scatter(df, x=cases, y=deaths, size=deaths, color=name, hover_name=name, log_x=True, size_max=60, title ="States Based on Total Number of Positive Cases Recorded against Deaths")
fig.show()

Visualization of Covid 19 Confirmed Cases Using Map of INDIA

In [29]:
plot = px.choropleth(df,
    geojson="https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson",
    featureidkey='properties.ST_NM',
    locations='State/UnionTerritory',
    color='Confirmed',
    color_continuous_scale='Reds',animation_frame=dt)
plot.update_geos(fitbounds="locations", visible=False)
plot.show()

Visualization of Covid 19 Cases Using Map of INDIA

In [21]:
plot = px.choropleth(df,
    geojson="https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson",
    featureidkey='properties.ST_NM',
    locations='State/UnionTerritory',
    color='Deaths',
    color_continuous_scale='Reds',animation_frame=dt)
plot.update_geos(fitbounds="locations", visible=False)
plot.show()
In [ ]: